<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    public function get($howMany) {
        $query = $this->db->query('SELECT news_date_posted, news_title FROM tb_news ORDER BY news_date_posted LIMIT ' . $howMany);
        return $query->result();
    }
    public function topFiveNews() {
        $query = $this->db->query("
            SELECT news_id_pk as news_id, news_date_posted AS posted, news_title AS title FROM tb_news
            WHERE news_barangay_id_fk=".$this->session->userdata('barangay_id')."
            ORDER BY news_date_posted
            LIMIT 5;
        ");
        return $query->result();
    }
    public function topNews() {
        if($this->session->userdata('barangay_id') == '' || $this->session->userdata('barangay_id') == null) {
            $query = $this->db->query("
                SELECT news_id_pk as news_id, news_title AS title FROM tb_news 
                ORDER BY news_date_posted
                LIMIT 5;
            ");
        } else {
            $query = $this->db->query("
                SELECT news_id_pk as news_id, news_title AS title FROM tb_news 
                WHERE news_barangay_id_fk = "
                . $this->session->userdata('barangay_id') . "
                ORDER BY news_date_posted
                LIMIT 5;
            ");
        }
        return $query->result();
    }
    public function getAll() {
        $query = $this->db->query("
            SELECT * FROM tb_news
            WHERE news_barangay_id_fk = " .
            $this->session->userdata('barangay_id') .
            " ORDER BY news_date_posted DESC
        ");
        return $query->result();
    }
    public function insertNews() {
        $query = $this->db->query("
            INSERT INTO tb_news
            VALUES(NULL, "
                .$this->session->userdata('barangay_id').", "
                .$this->session->userdata('user_id').", '"
                .$this->input->post('l_title')."', '"
                .$this->input->post('l_body')."', "
                ."NULL, SYSDATE())
        ");
        return $this->db->insert_id();
        //return $query;
    }
    public function getNewsId($e=0) {
        //$query = $this->db->getwhere('tb_sitios', array('sitio_id_pk'=>$e));
        $query = $this->db->query("
            SELECT * FROM tb_news
            WHERE news_id_pk="
            .$e." AND news_barangay_id_fk="
            .$this->session->userdata('barangay_id')
            ." LIMIT 1
        ");
        //return $query->result();
        return $query->row_array();
    }
    public function deleteNews($e=0) {
        $query = $this->db->query("
            DELETE FROM tb_news
            WHERE news_id_pk="
            .$e." AND news_barangay_id_fk="
            .$this->session->userdata('barangay_id')."
        ");
        return $query;
    }
    public function updateNews() {
        $query = $this->db->query("
            UPDATE  tb_news SET news_title =  '"
            .$this->input->post('l_title')."',
            news_body =  '"
            .$this->input->post('l_body')."'
            WHERE news_id_pk ="
            .$this->input->post('l_id')." LIMIT 1 ;
        ");
        return 'ok';
    }
    public function addNews() {
        $query = $this->db->query("
            INSERT into tb_news 
                VALUES (null,".$this->session->userdata('barangay_id').", ".$this->session->userdata('user_id').",'".$this->input->post('l_title')."','".$this->input->post('l_body')."','".$this->input->post('l_news_photo_path')."','".$this->input->post('l_news_date_posted')." ')");
        return 'ok';
    }
}